# To change this template, choose Tools | Templates
# and open the template in the editor.
from panda3d.core import *
from direct.showbase import Audio3DManager
from panda3d.physics import *
from direct.interval.ParticleInterval import *
from direct.particles.Particles import Particles
from direct.particles.ParticleEffect import ParticleEffect

import random, sys, os, math,time

class Weapon():
    def __init__(self,name,type,damage,model,offset,heading,rate,reload,ammo,clip,mclip,canscope,isscoped,spray,recoil,auto,fsound,scale = Vec3(0.01,0.01,0.01)):
        random.seed(time.clock())
        self.name = name
        self.type = type
        self.damage = damage
        self.model = model
        self.baseoffset = offset
        self.offset = self.baseoffset
        self.heading = heading
        self.rate = rate
        self.reload = reload
        self.ammo = ammo
        self.clip = clip
        self.maxclip = mclip
        self.canscope = canscope
        self.isscoped = isscoped
        self.spray = spray
        self.recoil = recoil
        self.automatic = auto
        self.canfire = True
        self.lastfired = 0
        self.firesound = base.loader.loadSfx(fsound)
        self.bulletcount = 0
        self.scale = scale
    def Fire(self):
        time = globalClock.getFrameTime()
        if time - self.lastfired >= self.rate:
            if self.automatic == False:
                if self.canfire:
                    if self.clip > 0:
                        self.lastfired = time
                        self.firesound.play()
                        self.BulletMath()
                        #print "bullet" + str(time)
                        self.canfire = False
                        self.clip -= 1
                    else:
                        self.Reload()
            else:
                if self.clip > 0:
                    self.BulletMath()
                    self.firesound.play()
                    self.lastfired = time
                    #print "bullet" + str(time)
                    self.clip -= 1
                else:
                    self.Reload()
    def Reload(self):
        self.clip += self.maxclip
        print "reloading"
    def Drop(self):
        self.model = ""

    def BulletMath(self):
        randomSpray = random.randint(-self.spray,+self.spray)
        randomRecoil = random.randint(0,self.recoil)
        player = base.Player1
        base.Cam1.pitch += randomRecoil/2
        base.Cam1.heading -= randomSpray/2
        self.offset = self.baseoffset - Vec3(0,0.002,-0.001)
        pFrom = Point3(player.dummy.getPos()+Vec3(player.dummy.getMat().getRow3(1))*1 )
        dir =  Vec3(player.dummy.getMat().getRow3(1))
        result = base.Physics1.doRaycast(pFrom, dir, 9999)
        if not result.isEmpty():
            try:
                if "Ladder" in result.getShape().getActor().getName():
                    print "Ladder"
                elif "Terrain" in result.getShape().getActor().getName():
                    print "Map"
    #                g = ParticleEffect()
    #                g.setLightOff()
    #                g.setShaderOff()
    #                g.loadConfig("particles/bullet.ptf")
    #                g.setPos(result.getImpactPos())
    #                g.start()
    #                g.reparentTo(render)
    #                self.bulletcount+=1
    #                if self.bulletcount >= 2:
    #                    a = self.bulletcount-1
    #                    b=render.find("particle-effect-"+str(a))
    #                    if b:
    #                        b.removeNode()
                else:
                    if base.client.ClientList[result.getShape().getActor().getName()].team != base.Player1.team:
                        base.client.writeTask("HIT",result.getShape().getActor().getName())
                    else:
                        print "TEAM KILL _DEV_"
            except:
                print "Collision error _DEV_"



